package com.wanda.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wanda.model.CustomerModel;
import com.wanda.model.JsonGeneralModel;
import com.wanda.service.CustomerService;
import com.wanda.util.DateFormat;


@Controller
@RequestMapping("qt/Customer")
public class CustomerController {
	
	@Resource
	private CustomerService customerService;
	
  
	//添加客户 
	@RequestMapping(value="/addCustomer.do")
    public @ResponseBody Map<String,Object> add(String customer_name,String sex,String tel,String describel,String customer_type,String maritalStatus,String address, String id_number,String profession,String income_level ){
		System.out.println("进入登录"); 
		CustomerModel customer=new CustomerModel(); 
		
		customer.setCustomer_name(customer_name);
		customer.setSex(sex);
		customer.setCustomer_type(customer_type);   
		customer.setTel(tel);
		customer.setDescribel(describel); 
        
		customer.setId_number(id_number)   ;
		customer.setProfession(profession);  
		customer.setIncome_level(income_level);

		customer.setUpdate_time(DateFormat.getNowDate());
		customer.setDelete_user(" ");
		//customer.setDelete_time(DateFormat.getNowDate());
		customer.setDelete_state("0");
		customer.setMarital_status(maritalStatus);
		customer.setAddress(address); 	
	    customerService.addCustomer(customer);
    	Map<String,Object> map = new HashMap<String,Object>();  
    	System.out.println("添加成功"); 
    	map.put("msg","成功");
    	return map;
    }
	
	
	//修改客户
		@RequestMapping(value="/updateCustomer.do")
	    public @ResponseBody Map<String,Object> update(String customer_id,String customer_name,String sex,String tel,String describel,String customer_type,String update_user,String maritalStatus,String address, String id_number,String profession,String income_level){
		     System.err.println("tel:"+tel); 
		     System.err.println("customer_name:"+customer_name);
		     System.err.println("customer_id:"+customer_id);
			CustomerModel customer=new CustomerModel();
			customer.setCustomer_id(customer_id);
			customer.setCustomer_name(customer_name);
			customer.setSex(sex);
			customer.setId_number(id_number)   ;
			customer.setProfession(profession);  
			customer.setIncome_level(income_level);
			customer.setTel(tel);
			customer.setDescribel(describel);
			customer.setCustomer_type(customer_type);
			customer.setUpdate_user(update_user);
			customer.setUpdate_time(DateFormat.getNowDate());
			//customer.setDelete_user(delete_user);
			//customer.setDelete_time(DateFormat.getNowDate());
			customer.setDelete_state("0");
			customer.setDescribel(describel);
			customer.setMarital_status(maritalStatus);
			customer.setAddress(address); 
			customerService.updateCustomer(customer);
	    	Map<String,Object> map = new HashMap<String,Object>();  
	    	System.out.println("修改成功"); 
	    	
	    	return map;
	    }
		
		
		//删除客户
		@RequestMapping(value="/deleteCustomer.do")
			 public @ResponseBody Map<String,Object> delete(String customer_id){
				      
					CustomerModel customer=new CustomerModel();
					customer.setCustomer_id(customer_id);
					String delete_state="1"; 
					customer.setDelete_state(delete_state);
					
					
					customerService.deleteCustomer(customer);
			    	Map<String,Object> map = new HashMap<String,Object>();  
			    	System.out.println("删除成功"); 
			    	;
			    	return map;
			    }
				
		//按照ID查询
			@RequestMapping(value="/selectById.do")
			  public @ResponseBody JsonGeneralModel select(String customer_id){				
				    	 System.err.println(customer_id);
				    	 
				     	JsonGeneralModel jsonModel = new JsonGeneralModel();
				    	List<CustomerModel> customer =null;
						try {
							customer=customerService.selectById(customer_id);
							//System.out.println("customer:"+customer.get(0).getCustomer_name());
							jsonModel.setRows(customer); 
						} 
						catch (Exception e) {
							e.printStackTrace();
						}
						
						return jsonModel;
				    } 
				 
		  //查询所有
				 @RequestMapping(value="/selectAll.do")
				    public @ResponseBody JsonGeneralModel selectAll(){
					 System.out.println("查询所有");
						JsonGeneralModel<CustomerModel> jsonModel = new JsonGeneralModel();
						List<CustomerModel> customers =null;
						try {
							String delete_state="0"; 
							customers=customerService.selectAll(delete_state);
							jsonModel.setRows(customers);
						}catch (Exception e) {
							e.printStackTrace();
						}
						return jsonModel;
				    }
		//拼接sql查询
				 @RequestMapping(value="/selectBySql.do")
				    public @ResponseBody JsonGeneralModel selectBySql(int page,int rows,String SearchSql){
					 System.out.println("SQL查询");
					 System.err.println("SearchSql:"+SearchSql);
						JsonGeneralModel<CustomerModel> jsonModel = new JsonGeneralModel();
						List<CustomerModel> customers =null;
						int pageIndex=(page-1)*rows;
						int pageSize=0;
						try {
							 String delete_state="0";
							customers=customerService.selectBySql(pageIndex,rows,SearchSql);
							pageSize=customerService.selectAll(delete_state).size() % rows ==0?
									 customerService.selectAll(delete_state).size() / rows:
									 customerService.selectAll(delete_state).size() / rows +1;
				
					    			 jsonModel.setPage(page);
									 jsonModel.setRecords(customerService.selectAll(delete_state).size());
									 jsonModel.setTotal(pageSize);
									 Map<String,Object> data=new HashMap<String,Object>();
									 data.put("rows",customers);
									 jsonModel.setRows(customers);
						}catch (Exception e) {
							e.printStackTrace();
						}
						return jsonModel;  
				    }	 
					//拼接sql查询
				 @RequestMapping(value="/selectBySala.do",produces="text/html;charset=UTF-8")
				    public @ResponseBody String selectBySala(String Search){
					 System.out.println("SQL查询");
					 System.err.println("selectBySala SearchSql:"+Search);
						JsonGeneralModel<CustomerModel> jsonModel = new JsonGeneralModel();
						List<CustomerModel> customers =null;
						String word = null;
						int pageSize=0;
						try {
							 String delete_state="0";
							customers=customerService.selectBySql(pageSize,10000,Search);
						 word = "<words>";
							for(CustomerModel c:customers){
							  word +="<word cid=\""+c.getCustomer_id()+"\" tel=\""+c.getTel()+"\">"+c.getCustomer_name()+"</word>";
							  
								
							}
							word =word+"</words>";
					
						}catch (Exception e) {
							e.printStackTrace();
						}
						return word;  
				    }	
}
